<template>
  <div class="departments-container">
    <div class="app-container">
      <!-- 卡片组件 -->
      <el-card class="box-card">
        <!-- 使用 Tabs 组件完成标签页布局 -->
        <el-tabs v-model="activeName">
          <el-tab-pane label="组织架构" name="first" class="tab-pane">
            <!-- 使用 Layout 布局绘制头部区域 -->
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              class="department-header"
            >
              <el-col :span="20">
                <i class="el-icon-s-home" />
                <span class="company">江苏传智播客教育科技股份有限公司</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <el-col>负责人</el-col>
                  <el-col>
                    <el-dropdown>
                      <span class="el-dropdown-link">
                        操作<i class="el-icon-arrow-down el-icon--right" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="addBtn('')">添加子部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </el-tab-pane>
        </el-tabs>
        <!-- 树状结构 -->
        <div class="tab-pane aaa">
          <el-tree
            default-expand-all
            :data="List"
            :props="defaultProps"
          >
            <template v-slot="{ data }">
              <el-row type="flex" justify="space-between" style="width: 100%">
                <el-col :span="20">{{ data.name }}</el-col>
                <el-col :span="4">
                  <!-- <span>操作</span> -->
                  <el-row
                    type="flex"
                    justify="space-between"
                    style="width: 100%"
                  >
                    <el-col><span>{{ data.manager }}</span></el-col>
                    <el-col>
                      <el-dropdown>
                        <span class="el-dropdown-link">
                          操作<i class="el-icon-arrow-down el-icon--right" />
                        </span>
                        <el-dropdown-menu slot="dropdown">
                          <el-dropdown-item @click.native="addBtn(data.id)">添加子部门</el-dropdown-item>
                          <el-dropdown-item @click.native="setBtn(data.id)">编辑部门</el-dropdown-item>
                          <el-dropdown-item @click.native="delBtn(data.id)">删除部门</el-dropdown-item>
                        </el-dropdown-menu>
                      </el-dropdown>
                    </el-col>
                  </el-row>
                </el-col>
              </el-row>
            </template>
          </el-tree>
        </div>
        <el-dialog
          title="提示"
          :visible.sync="showdialog"
          width="50%"
          :close-on-click-modal="false"
          :close-on-press-escape="false"
        >
          <Dialog v-if="showdialog" :new-list="newList" :alllist="List" :isedit="isEdit" :parentid="parentid" @closepopUp="closepopUpFn" /></el-dialog>
      </el-card>
    </div>
  </div>
</template>

<script>
//  * 获取部门列表 *
import { getDepartment, delMout } from '@/api/departments'
import Dialog from './dialog.vue'
// 数组转换树结构
import { listToTree } from '@/utils'
export default {
  name: 'Departments',
  components: {
    Dialog
  },
  data() {
    return {
      activeName: 'first', // 被激活的 Tab 标签页
      List: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      showdialog: false, // 控制对话层显示隐藏
      parentid: '',
      isEdit: false, // 控制当前时修改还是删除
      newList: [] // 处理后的新数组

    }
  },
  created() {
    // 获得数据渲染到页面
    this.getDepartment()
  },
  methods: {
    closepopUpFn(bool) {
      this.showdialog = bool
      this.getDepartment()
    },

    async  getDepartment() {
      const res = await getDepartment()
      console.log(res)
      this.List = listToTree(res.data.depts)
      // 整理数据
      this.newList = res.data.depts.map(item => ({
        code: item.code,
        name: item.name,
        pid: item.pid,
        id: item.id
      }))
    },
    addBtn(id) {
      // 点击出现弹出框
      // console.log(1)
      this.showdialog = true
      this.parentid = id
      this.isEdit = false
    },
    setBtn(id) {
      // 点击修改部门
      this.showdialog = true
      this.isEdit = true
      this.parentid = id
    },
    // 删除部门
    async delBtn(id) {
      const res1 = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      console.log(res1)
      if (res1 === 'confirm') {
        const res = await delMout(id)
        console.log(res)
        this.getDepartment()
        this.$message.success(res.message)
      } else if (res1 === 'cancel') {
        this.$message.info('已取消')
      }
    }

  }
}
</script>

<style lang="scss" scoped>
.box-card {
  padding: 10px 30px;
}

.tab-pane {
  padding: 20px 80px;

  .company {
    margin-left: 12px;
    font-weight: bold;
    font-size: 14px;
  }

  .department-header {
    height: 50px;
    display: flex;
    align-items: center;
    border-bottom: 1px solid #ccc;
  }
}
.aaa {
  font-weight: 700;
}
</style>
